<template>
    <!-- 小区管理 -->
    <div class="scheme">
        <div class="header-box">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="小区名称：">
                    <el-input v-model="formInline.name" placeholder="请输入小区名称"></el-input>
                </el-form-item>
                <el-form-item label="所属门店:">
                    <el-select v-model="formInline.store_id" placeholder="请选择">
                        <el-option label="全部" value=""></el-option>
                        <el-option :label="item.name" :value="item.id" v-for="item in storeList" :key="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="submitForm">搜索</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" icon="el-icon-plus" @click="add">新增</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="tables">
            <el-table border :data="tableData" max-height="650" highlight-current-row :row-style="{height: '20px'}"
                      :header-cell-style="{background:'#e6e6e6',color:'#000000'}">
                <el-table-column prop="id" align="center" label="序号" width="60"></el-table-column>
                <el-table-column prop="name" align="center" label="名称"></el-table-column>
                <el-table-column prop="store_name" align="center" label="所属门店"></el-table-column>

                <el-table-column prop="status" align="center" label="状态">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'"
                                disable-transitions>{{ scope.row.status === 1 ? '启用' : '禁止' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="created_at" align="center" label="创建时间">
                </el-table-column>
                <el-table-column align="center" label="操作" width="280">
                    <template slot-scope="scope">
                        <el-button round size="mini" type="success" plain icon="el-icon-edit"
                                   @click="handleEdit(scope.row)">
                            编辑
                        </el-button>
                        <el-button round size="mini" type="danger" plain icon="el-icon-delete"
                                   @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                               :current-page.sync="page" :page-sizes="[15, 50, 100]" :page-size="limit"
                               layout="total, sizes, prev, pager, next, jumper" :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 新增弹框 -->
        <el-dialog :title="title" :visible.sync="dialogDistrict" width="60%" :show-close="false"
                   :close-on-click-modal="false" :close-on-press-escape="false">
            <el-form :model="form" label-width="10%">
                <el-form-item label="名称:">
                    <el-input v-model="form.name" style="width: 60%;"></el-input>
                </el-form-item>
                <el-form-item label="所属门店:">
                    <el-select size="mini" v-model="form.store_id" placeholder="请选择" style="width: 30%;">
                        <el-option :label="item.name" :value="item.id" v-for="item in storeList" :key="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态：">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">启用</el-radio>
                        <el-radio :label="2">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogDistrict = false">取 消</el-button>
                <el-button type="primary" @click="submitDialogForm">确 定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<script>
import {isEmpty} from "../../../api/http";

let that;
export default {
    name: "patient",
    data() {
        return {

            formInline: {
                name: "",
                store_id: "",
            },
            form: {
                name: "",
                store_id: "",
                status: "",
            },
            tableData: [
                {id: 1, name: '张三', phone: '13401230123', time: '2023-10-25 12:01:25', status: 1},

            ],
            dialogDistrict: false, //默认新增课程弹框隐藏
            title: "新增 ", //默认新增
            isEdit: false, //默认为新增
            page: 1, //默认第一页
            limit: 15, //默认一页显示10条数据
            total: 0,
            lat: '',
            lon: '',
            storeList: [{id: 1, name: "体能课程"}, {id: 2, name: "私教课程"}, {id: 3, name: "体考课程"}, {id: 4, name: "周末研学"}]

        };
    },
    created(){
        this.getStoreList();
        this.getDistrictList();
        that = this
    },
    activated(){
        this.getStoreList();
        this.getDistrictList();
        that = this
    },
    methods: {

        // 点击新增课程按钮
        add() {
            this.title = "新增";
            this.dialogDistrict = true;
            this.isEdit = false;
            this.initForm();
        },
        getStoreList() {

            let params = {
                status: 1,
                page: 1,
                per_page: 10000,
            };
            this.$get(this.$api.storeApi, params).then((res) => {
                if (res.code == 200) {
                    this.storeList = res.data.data;
                } else {
                    return this.$message.error(res.msg);
                }
            });
        },
        getDistrictList(page, limit) {
            let params = {
                page: page,
                per_page: limit,
                name: this.formInline.name,
                store_id:this.formInline.store_id,
            };
            this.$get(this.$api.districtApi, params).then((res) => {
                if (res.code == 200) {
                    this.tableData = res.data.data;
                    this.total = res.data.total;
                } else {
                    return this.$message.error(res.msg);
                }
            });
        },

        // 点击编辑按钮
        handleEdit(row) {
            this.title = "编辑课程";
            this.id = row.id;
            this.form.name = row.name;
            this.form.store_id = row.store_id;
            this.form.status = row.status;
            this.dialogDistrict = true;
            this.isEdit = true;
        },
        // 点击编辑弹框取消按钮
        goToEdit() {
            this.dialogDistrict = false;
            this.isEdit = false;
        },

        // 点击删除按钮
        handleDelete(row) {
            this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.$delete(this.$api.districtApi + "/" + row.id).then((res) => {
                        if (res.code == 200) {
                            this.getDistrictList(this.page, this.limit);
                            return this.$message.success("删除成功！");
                        } else {
                            return this.$message.error(res.msg);
                        }
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        // 分页
        handleSizeChange(size) {
            this.limit = size;
            this.getDistrictList(this.page, this.limit);
        },
        handleCurrentChange(index) {
            this.page = index;
            this.getDistrictList(this.page, this.limit);
        },
        // 点击查询按钮
        submitForm() {
            this.getDistrictList(this.page, this.limit);
        },
        initForm() {
            this.form = {
                name: "",
                store_id: "",
                status: "",
            }

        },
        // 提交课程数据确定按钮
        submitDialogForm() {
            console.log(this.form)
            if (this.form.name == "") {
                return this.$message.error("请输入名称！");
            }
            if (this.form.store_id == "") {
                return this.$message.error("请选择门店！");
            }
            if (this.form.status == "") {
                return this.$message.error("请选择状态！");
            }
            let params = {
                name: this.form.name,
                store_id: this.form.store_id,
                status: this.form.status,
            };
            if (this.isEdit) {
                this.$put(this.$api.districtApi + "/" + this.id, params).then(
                    (res) => {
                        if (res.code == 200) {
                            this.initForm();
                            this.dialogDistrict = false;
                            this.getDistrictList(this.page, this.limit);
                            return this.$message.success("编辑成功！");
                        } else {
                            return this.$message.error(res.msg);
                        }
                    }
                );

            } else {
                this.$post(this.$api.districtApi, params).then(
                    (res) => {
                        if (res.code == 200) {
                            this.initForm();
                            this.dialogDistrict = false;
                            this.getDistrictList(this.page, this.limit);
                            return this.$message.success("新增成功！");
                        } else {
                            return this.$message.error(res.msg);
                        }
                    }
                );
            }
        },
    },
};
</script>

<style scoped>
.pagination {
    text-align: center;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.is-selected {
    color: #1989FA;
}
</style>
